package com.vframework.system.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.util.StringUtil;
import com.vframework.base.controller.BaseController;
import com.vframework.common.constant.SessionConstant;
import com.vframework.system.demo.model.DemoBean;
import com.vframework.system.demo.service.DemoService;

/***************************************************************
 * Name:        DemoController.java
 * Create At:   2018年5月24日
 * Module:      
 * Description: [系统开发demo]
 * @author      ztshan
 * ------------------------------------------------------------
 * 修改历史
 * 序号         日期                       修改人               修改原因
 * 1    2018年5月24日      ztshan    
 * 2
***************************************************************/
@Controller
@RequestMapping("DemoController")
public class DemoController extends BaseController {
    
    private static final String BASE_PATH = "com/vframework/system/demo/pages/";
    
    @Autowired
    private DemoService demoService;
    
    /**
     * @author   ztshan
     * @description  begin
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "begin")
    public String begin(Model model)throws Exception {
        return queryList(model);
    }
    
    /**
     * @author   ztshan
     * @description  queryList
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "queryList")
    private String queryList(Model model) throws Exception {
        try {
            List<DemoBean> resultList = null;
            int pageCount = demoService.getCount(super.paramMap);
            if (pageCount > 0) {
                super.setPageInfo(pageCount);
                resultList = demoService.getList(super.paramMap);
            }
            //页面list展示用
            model.addAttribute("resultList", resultList);
            //翻页用
            request.setAttribute("pageCount", pageCount);
            request.setAttribute("actionurl", "DemoController/queryList.do");
        } catch (Exception e) {
            super.message("查询失败" + e.getMessage());
            return BASE_PATH + "demoList";
        }
        return BASE_PATH + "demoList";
    }

    @RequestMapping(value = "dialogList")
    private String dialogList(Model model) throws Exception {
        try {
            List<DemoBean> resultList = null;
            int pageCount = demoService.getCount(super.paramMap);
            if (pageCount > 0) {
                super.setPageInfo(pageCount);
                resultList = demoService.getList(super.paramMap);
            }
            //页面list展示用
            model.addAttribute("resultList", resultList);
            //翻页用
            request.setAttribute("pageCount", pageCount);
            request.setAttribute("actionurl", "DemoController/dialogList.do");
        } catch (Exception e) {
            super.message("查询失败" + e.getMessage());
            return BASE_PATH + "dialogList";
        }
        return BASE_PATH + "dialogList";
    }
    
    @RequestMapping(value = "edit")
    private String edit(Model model) throws Exception {
        try {
            String demoId = request.getParameter("demoId");
            DemoBean demoBean = null;
            if (StringUtil.isEmpty(demoId)) {
                demoBean = new DemoBean();
                model.addAttribute("method", "add");
            } else {
                demoBean = demoService.getById(demoId);
                model.addAttribute("method", "edit");
            }
            model.addAttribute("demoBean", demoBean);
        } catch (Exception e) {
            super.message("查询失败: " + e.getMessage());
            return queryList(model);
        }
        return BASE_PATH + "demoAdd";
    }
    
    @RequestMapping(value = "addSave")
    private String addSave(DemoBean demoBean, Model model) throws Exception {
        try {
            demoBean.setCreateBy((String)session.getAttribute(SessionConstant.SESSION_USER_ID));
            int res = demoService.insertDemo(demoBean);
            if (res > 0)
                super.message("保存成功");
            else 
                super.message("保存失败");
            demoService.insertLog(super.MakeLogBean(super.LOG_ADD_KEY, "添加demo", demoBean.toString()));
        } catch (Exception e) {
            super.message("保存失败: " + e.getMessage());
            return queryList(model);
        }
        return queryList(model);
    }

    @RequestMapping(value = "editSave")
    private String editSave(DemoBean demoBean, Model model) throws Exception {
        try {
            demoBean.setUpdateBy((String)session.getAttribute(SessionConstant.SESSION_USER_ID));
            int res = demoService.updateDemo(demoBean);
            if (res > 0)
                super.message("修改成功");
            else 
                super.message("修改失败");
            demoService.insertLog(super.MakeLogBean(super.LOG_UPDATE_KEY, "修改demo", demoBean.toString()));
        } catch (Exception e) {
            super.message("修改失败: " + e.getMessage());
            return queryList(model);
        }
        return queryList(model);
    }
    
    @RequestMapping(value = "del")
    private String del(Model model) throws Exception {
        try {
            String[] ids = request.getParameterValues("ids");
            int res = demoService.deleteDemo(ids);
            if (res > 0)
                super.message("成功删除" + res + "条");
            else 
                super.message("删除失败!");
        } catch (Exception e) {
            super.message("删除失败： " + e.getMessage());
            return queryList(model);
        }
        return queryList(model);
    }
}